import jade.wrapper.AgentController;

import org.lsk.pfc.scsim.model.JadeContainerFactory;
import org.lsk.pfc.scsim.model.agent.TimerAgent;
import org.lsk.pfc.scsim.model.mas.CustomerMAS;
import org.lsk.pfc.scsim.model.mas.MultiAgentSystem;
import org.lsk.pfc.scsim.model.messages.MessageConstants;
import org.lsk.pfc.scsim.model.util.NetworkHelper;

/**
 * File Runner.java created by Pablo Díaz Fernández
 * on 09/12/2011 for the project supply-chain-simulator
 */

/**
 * @author Pablo Díaz Fernández
 * 
 */
public final class Runner {

	/**
	 * Private constructor to avoid instantiation.
	 */
	private Runner() {
	}

	/**
	 * 
	 * @param args .
	 */
	public static void main(final String[] args) {

		Process p = null;
		try {
			String[] cmdarray = new String[] { "java", "-classpath",
			        System.getProperty("java.class.path"),
			        "jade.Boot", "-gui", "-host",  NetworkHelper.getLocalIP()};

			p = Runtime.getRuntime().exec(cmdarray);
			
			JadeContainerFactory factory = JadeContainerFactory.getInstance();

			AgentController timerAgent = factory.getDefaultContainer()
			        .createNewAgent(MessageConstants.TIMER_AGENT_NAME,
			                TimerAgent.class.getCanonicalName(), null);

			MultiAgentSystem customer = new CustomerMAS("customer",
			        factory.getDefaultContainer());
//			MultiAgentSystem retailer = new BasicMAS("retailer",
//			        factory.getDefaultContainer());
//			MultiAgentSystem wholesaler = new BasicMAS("wholesaler",
//			        factory.getDefaultContainer());
//			MultiAgentSystem distributor = new BasicMAS("distributor",
//			        factory.getDefaultContainer());
//			MultiAgentSystem manufacturer = new ManufacturerMAS("manufacturer",
//			        factory.getDefaultContainer());

			customer.startAgents();
//			retailer.startAgents();
//			wholesaler.startAgents();
//			distributor.startAgents();
//			manufacturer.startAgents();

			timerAgent.start();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			p.destroy();
			Runtime.getRuntime().exit(1);
			
		}		
		
		//Pause for 10 seconds
        try {
	        Thread.sleep(20000);
        } catch (InterruptedException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
		p.destroy();
		Runtime.getRuntime().exit(0);
	}
}
